package com.dao.impl;

import com.bean.Menu;
import com.bean.Role;
import com.bean.Users;
import com.dao.DBUtils;
import com.dao.MenuDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtils implements MenuDao {
    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from menu";
            resultSet = query(sql,null);
            while (resultSet.next()){
               Menu menu = new Menu();
               menu.setMenuId(resultSet.getInt("menuid"));
               menu.setMenuName(resultSet.getString("menuname"));
               menu.setState(resultSet.getInt("state"));
               menu.setUpMenuId(resultSet.getInt("upmenuid"));
               menu.setDesc(resultSet.getString("desc"));
               menu.setUrl(resultSet.getString("url"));
               menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public List<Menu> getMenusList(int pageIndex,int pageSize) {
        List<Menu> menuList = new ArrayList<Menu>();
        String sql="select * from menu limit ?,?";
        try {
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql,params);
            while (resultSet.next()){
                //1.取出各表数据
                Menu menu = new Menu();
               menu.setMenuName(resultSet.getString("menuname"));
               menu.setUrl(resultSet.getString("url"));
               menu.setState(resultSet.getInt("state"));
               menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return  menuList;
    }

    @Override
    public int total() {
        int total = 0;
        String sql = "select count(1) from menu  ";
        try {
            resultSet = query(sql, null);
            while (resultSet.next()) {
                total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }
}
